package org.springframework.core.util;

import org.springframework.beans.factory.anno.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/12/14
 */
public class AnnotatedElementUtils {

    public static boolean isAnnotated(AnnotatedElement element, Class<? extends Annotation> annotationType){
        if (element.isAnnotationPresent(annotationType)) {
            return true;
        }
        return false;
    }

    public static Map<String, Object> getAnnotationAttributes(AnnotatedElement element,
                                                              Class<? extends Annotation> annotationType,
                                                              boolean classValuesAsString) {
        Annotation anno = element.getAnnotation(annotationType);

        Map<String, Object> attributesMap = new HashMap<>();
        if(null==anno)return attributesMap;
        Method[] methods = annotationType.getDeclaredMethods();
        for (Method method:methods) {
            try {
                attributesMap.put(method.getName(),method.invoke(anno));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return attributesMap;
    }

    public static void main(String[] args) {
        getAnnotationAttributes(A.class,Component.class,false);
    }

    @Component("sdafasdfsda")
    class A{

    }
}
